# -*- coding: utf-8 -*-
# 找目标 vin 列表中未人车绑定的 vin
# author yeyi
# date 2022-01-24

import xlwt
import xlrd
import pymongo
import csv
import bson
import time
import traceback
import requests
import json

def printt(s):
    print(s)
    f = open('relationProfile.log', 'a+', encoding='utf-8')
    print(s,file=f)
    f.close()


printt('start: '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

# relation mongo 数据库地址
conn = pymongo.MongoClient("mongodb://mongouser:tce%40guangqi2019@10.123.33.12:27017/?authSource=admin")
db = conn['iov_account']
table = db.relationProfile

rxls = xlwt.Workbook()  # 新建工作簿
rxls.add_sheet('Sheet1', cell_overwrite_ok=True)
rxls.add_sheet('Sheet')

# 结果输出文件
f1 = open('relationProfile.csv', 'w', encoding='utf-8')
csv_writer_1 = csv.writer(f1)
csv_writer_1.writerow(['din'])

f = open(r"relationProfile-din.txt")
line = f.readline()
iccids = []
while line:
    try:
        din = line.split()[0]
        line = f.readline()
        result = table.find({
            "id2":din
        })

        results=list(result)
        size = len(results)
        printt('size: '+str(size))
        if size<0:
            line = [din]
            csv_writer_1.writerow(line)
    except:
        # 打印错误信息
        printt('for err: '+str(re))
        traceback.print_exc()

f.close()

printt('end: '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))